BKBridge = window.BKBridge || {
    callbacksCount:0,  callbacks:{},
    ResultCallBack:function (callbackId, resultArray) {
        try {
            var callback = BKBridge.callbacks[callbackId];
            if (!callback) {
                return
            }
            resultArray=decodeURIComponent(resultArray);
            callback(resultArray);
        }catch(e) {alert(e)}
    },
    Call:function (functionName, args, callback) {
        var hasCallback = callback && typeof callback == "function";
        var callbackId = hasCallback ? ++BKBridge.callbacksCount : 0;
        if (hasCallback)BKBridge.callbacks[callbackId] = callback;
        if(BKBridge.browser().ios){
            if(window["webkit"] && window["webkit"]["messageHandlers"]){
                window.webkit.messageHandlers.JsBridge.postMessage("JsBridge:" + functionName + ":" + callbackId+ ":" + encodeURIComponent(JSON.stringify(args)));
            }else{
                BKBridge.FHMIFrame("JsBridge:" + functionName + ":" + callbackId+ ":" + encodeURIComponent(JSON.stringify(args)));
            }
        }else if(BKBridge.browser().android)
        {
            NativeBridge.JsBridge(functionName ,callbackId, encodeURIComponent(JSON.stringify(args)));
        }
    },
    FHMIFrame:function(value) {
        var FHMIFrame = document.createElement('iframe');
        FHMIFrame.style.display = 'none';
        FHMIFrame.setAttribute("src",value);
        document.documentElement.appendChild(FHMIFrame);
        FHMIFrame.parentNode.removeChild(FHMIFrame);
        FHMIFrame = null;
    },
    browser:function()
    {
        var u = navigator.userAgent;
        return {ios:!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1};
    }
}

if((typeof window.webkit == 'undefined') && (typeof NativeBridge == 'undefined')){
  BKBridge=null;
  window.BKBridge=null;
}


//=========================从这里开始看=========================

/**
 * 返回，相当于点APP左上角的返回按钮
 */
function bk_back () {
    if (window.BKBridge) {
        window.BKBridge.Call('go_back', {
                'data': "",
                'backLevel': 1
            },
            '');
    } else {
        window.history.back(-1);
    }
}


/**
 * 修改APP导航上的标题
 * @param title  标题
 */
function bk_set_nav_title(title) {
    if (window.BKBridge) {
        window.BKBridge.Call('bk_set_nav_title', {
                'title': title
            },
            '');
    } else {
        document.title = title;
    }
}

/**
 * 用新的页面打开一个网页
 * @param url    必填，网页的完成地址，带http的
 * @param title  可选，APP导航上的标题
 */
function bk_new_page (url, title) {
    var targetUrl = url;
    if(!title){
        title="";
    }
    if (window.BKBridge) {
        window.BKBridge.Call("new_page", {
                "url": targetUrl,
                "title": title
            },
            "");
    } else {
        window.location.href = url;
    }
}









